package cn.edu.hnu.gpg.service;

import cn.edu.hnu.gpg.dao.UploadedFileMapper;
import cn.edu.hnu.gpg.entity.UploadedFile;
import cn.edu.hnu.gpg.entity.UploadedFileExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class UploadedFileServiceImpl implements UploadedFileService {

    private UploadedFileMapper uploadedFileMapper;

    @Autowired
    public UploadedFileServiceImpl(UploadedFileMapper uploadedFileMapper){
        this.uploadedFileMapper = uploadedFileMapper;
    }

    @Override
    public boolean createUploadFile(String path, int user_id) {
        if (path.isEmpty() || user_id < 1)
            return false;

        UploadedFile uploadedFile = new UploadedFile();
        uploadedFile.setFileName(path);
        uploadedFile.setUploader(user_id);

        return uploadedFileMapper.insertSelective(uploadedFile) > 0;
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class, IOException.class})
    public Integer uploadFile(String dirPath, String fileName, MultipartFile file, int userId) throws IOException {

        if (fileName == null || file == null)
            throw new IllegalArgumentException("Argument must not be null");

        UploadedFile uploadedFile = new UploadedFile();
        uploadedFile.setFileName(file.getOriginalFilename());
        uploadedFile.setUploader(userId);
        uploadedFileMapper.insertSelective(uploadedFile);

        //上传
        if (dirPath == null)
            dirPath = "";

        File file1 = new File(dirPath, uploadedFile.getUploadedFileId().toString());
        if (!file1.getParentFile().exists())
            if (!file1.getParentFile().mkdirs())
                throw new RuntimeException("Error occurred in creating directory");
        if (!file1.exists())
            if (!file1.createNewFile())
                throw new RuntimeException("Error occurred in creating file");
        file.transferTo(file1);

        return uploadedFile.getUploadedFileId();
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class, IOException.class})
    public void replaceFile(String dirPath, Integer uploadedFileId, String fileName, MultipartFile file) throws IOException {

        if (uploadedFileId == null || file == null)
            throw new IllegalArgumentException("Argument must not be null");

        UploadedFile uploadedFile = uploadedFileMapper.selectByPrimaryKey(uploadedFileId);
        if (uploadedFile == null)
            throw new RuntimeException("Specified uploaded file not found");
        uploadedFile.setFileName(fileName);
        uploadedFile.setUploadTime(new Date());
        uploadedFileMapper.updateByPrimaryKey(uploadedFile);

        //上传
        if (dirPath == null || dirPath.isEmpty())
            dirPath = "";

        File file1 = new File(dirPath, uploadedFile.getUploadedFileId().toString());
        if (!file1.getParentFile().exists())
            if (!file1.getParentFile().mkdirs())
                throw new RuntimeException("Error occurred in creating directory");
        if (!file1.exists())
            if (!file1.createNewFile())
                throw new RuntimeException("Error occurred in creating file");
        file.transferTo(file1);
    }

    @Override
    public boolean deleteFile(String path) {
        if (path.isEmpty())
            throw new IllegalArgumentException("这不是一个有效的文件路径");

        UploadedFileExample uploadedFileExample = new UploadedFileExample();
        uploadedFileExample.or().andFileNameEqualTo(path);
        return uploadedFileMapper.deleteByExample(uploadedFileExample) > 0;
    }

    @Override
    public List<UploadedFile> getAllFile(int user_id) {
        if (user_id < 1 )
            return null;

        UploadedFileExample uploadedFileExample = new UploadedFileExample();
        uploadedFileExample.or().andUploaderEqualTo(user_id);
        return uploadedFileMapper.selectByExample(uploadedFileExample);
    }

    @Override
    public UploadedFile queryFileInfo(Integer fileId) {
        return uploadedFileMapper.selectByPrimaryKey(fileId);
    }
}
